package bpm.dao.impl.transfer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import bpm.domain.document.DocumentEntry;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class DocumentEntryMapExtractor implements ResultSetExtractor {

	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		Map<String, DocumentEntry> result = null;
		boolean initialized = false;
		while(rs.next()) {
			if(!initialized) {
				result = new HashMap<String, DocumentEntry>();
				initialized = true;
			}
			
			DocumentEntry entry = new DocumentEntry();
			entry.setId(rs.getString("id"));
			entry.setDocumentId(rs.getString("doc_id"));
			entry.setFieldKey(rs.getString("field_key"));
			entry.setValue(rs.getString("value"));
			java.sql.Date createTime = rs.getDate("create_time");
			if(createTime != null) {
				entry.setCreateTime(new Date(createTime.getTime()));
			}
			java.sql.Date lastUpdateTime = rs.getDate("last_update_time");
			if(lastUpdateTime != null) {
				entry.setLastUpdateTime(new Date(lastUpdateTime.getTime()));
			}
			entry.setDescription(rs.getString("description"));
			
			if(result != null) {
				result.put(entry.getFieldKey(), entry);
			}
		}
		
		return result;
	}
}
